package com.ygj.announcement.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.cjy.common.bean.OpAttributes;
import com.ygj.announcement.service.AnnouncementService;
import com.ygj.base.common.persistent.model.Notes;

/**
 * 
 * @author Kat Colin
 *
 */
@Action("/announcement")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class AnnouncementAction extends AbstractBaseAction{
	
	private static final long serialVersionUID = -2838359151810556777L;

	private static final Logger log = LoggerFactory.getLogger(AnnouncementAction.class);
	
	@Resource
	private AnnouncementService announcementService;
	
	private String rowguid;
	
	private int page;
	
	private String name;
	
	public String getRowguid() {
		return rowguid;
	}
	public void setRowguid(String rowguid) {
		this.rowguid = rowguid;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	@Action("/queryTotalCounts")
	public String queryTotalCounts() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("totalCounts", announcementService.queryAllTotalCounts())));
		} catch (Exception e) {
			log.error(e.toString());
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	@Action("/queryAll")
	public String queryAllAnnouncement() {
		try {
			List<Notes> notesList = new ArrayList<Notes>();
			notesList = announcementService.queryAll(page);
			System.out.println(notesList.size());
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", notesList));
		} catch (Exception e) {
			log.error(e.toString());
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	@Action("/queryDetail")
	public String queryDetailedInformationOfAnnouncement() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", announcementService.queryDetailedInformationOfAnnouncementByRowguid(rowguid.trim())));
		} catch (Exception e) {
			log.error(e.toString());
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	@Action("/queryOaPeople")
	public String queryOaPeople() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", announcementService.queryOaPeople(name)));
		} catch (Exception e) {
			log.error(e.toString());
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
}
